﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.InvestmeIBLL;
//using KingRoad.EPM.InvestmeEntity;
using KingRoad.EPM.Web.Util;
//using KingRoad.EPM.InvestmeBLL;
using KingRoad.EPM.ProjectInfoIBLL;
using KingRoad.EPM.ProjectInfoBLL;
using KingRoad.EPM.ProjectInfoEntity;
using DotNet.Model.MyEntity;
using DotNet.Model;
using DotNet.Service;
using System.IO;
using KingRoad.EPM.Web.ViewModels;

namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    public class InstructionsController : Controller
    {
        //
        // GET: /Permission/Instructions/
        private IProject_BrowserHistoryBLL _BrowserHistoryBLL = null;
        private IProject_InstructionsBLL _Instructions = null;
        public InstructionsController()
        {
            _BrowserHistoryBLL = new Project_BrowserHistoryBLL();
            _Instructions = new KingRoad.EPM.ProjectInfoBLL.Project_InstructionsBLL();
        }
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获得模块列表
        /// </summary>
        /// <returns>json数据</returns>
        public JsonResult GetModuleMenus()
        {
            //修改为获取当前登录人具有查看权限的模块 WS 2011.11.2
            List<BaseModuleEntity> list = ServiceManager.Instance.ModuleService.GetCurrentModules() as List<BaseModuleEntity>;
            return Json(list);
        }
        /// <summary>
        /// 数据列表
        /// </summary>
        /// <param name="id">模块ID</param>
        /// <returns></returns>
        public ActionResult InstructionsList(int id)
        {
            List<PROJECT_INSTRUCTIONS> list = _Instructions.GetAllListByModudleID(id) ?? new List<PROJECT_INSTRUCTIONS>();
            ViewBag.InstructionsList = list;
            return View();
        }
        /// <summary>
        /// 添加和编辑页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Creat()
        {
            ViewBag.filetype = KingRoad.EPM.Common.Configuration.Settings.Instance.MeasureFileType;
            ViewBag.fileSize = KingRoad.EPM.Common.Configuration.Settings.Instance.ReportlibFileSize * 1024;//文件大小单位字节
            ViewBag.filePath = "/Content/images/Instruction";
            return View();
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            PROJECT_INSTRUCTIONS model = _Instructions.GetEntityByProjID(id) ?? new PROJECT_INSTRUCTIONS();
            return View("Creat", model);
        }
        /// <summary>
        /// 删除方法 连同删除文件
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            //删除文件
            PROJECT_INSTRUCTIONS model = _Instructions.GetEntityByProjID(id) ?? new PROJECT_INSTRUCTIONS();
            //删除数据
            if (_Instructions.DeteleInstructions(id) > 0)
            {
                if (System.IO.File.Exists(Server.MapPath(model.Instructions_Path)))
                {
                    System.IO.File.Delete(Server.MapPath(model.Instructions_Path));
                }
                statuCode = "200";
                statuMessage = "删除成功！";
            }
            else
            {
                statuCode = "300";
                statuMessage = "删除失败，请重试！";
            }

            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode
            });
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        public string UploadFile(HttpPostedFileBase FileData)
        {
            string state = string.Empty;//1:上传文件类型不正确，2：上传文件大小超过限制，3：出现异常  
            string tempPath = "/Content/images/Instruction";//上传地址
            string tempType = KingRoad.EPM.Common.Configuration.Settings.Instance.MeasureFileType;//上传文件类型
            int tempSize = KingRoad.EPM.Common.Configuration.Settings.Instance.ReportlibFileSize;//上传文件大小限制
            if (FileData != null)
            {
                try
                {
                    string tempFile = FileData.FileName;
                    //这里做判断后缀名，取全局配置信息
                    string lastStr = tempFile.Substring(tempFile.LastIndexOf('.')).ToLower();

                    string[] tempList = tempType.Split(';');

                    if (!tempList.Contains(lastStr))
                    {
                        state = "1";
                    }
                    else if (FileData.ContentLength > tempSize * 1024 || FileData.ContentLength == 0)
                    {
                        state = "2";
                    }
                    else
                    {
                        string FileName = Guid.NewGuid().ToString() + lastStr;

                        if (!tempPath.EndsWith("/"))
                            tempPath += "/";

                        state = tempPath + FileName;

                        tempPath = Server.MapPath(tempPath);

                        if (!Directory.Exists(tempPath))
                            Directory.CreateDirectory(tempPath);

                        FileData.SaveAs(tempPath + FileName);
                    }
                }
                catch (Exception e)
                {
                    state = "3";
                }
            }
            return state;
        }
        /// <summary>
        /// 添加和编辑方法
        /// </summary>
        /// <param name="model">数据实体</param>
        /// <returns></returns>
        public ActionResult Creating(PROJECT_INSTRUCTIONS model)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            string exeType = string.Empty;
            //实现更新   
            if (ModelState.IsValid)
            {
                if (model.Instructions_ID != 0)
                {
                    PROJECT_INSTRUCTIONS modelView = _Instructions.GetEntityByProjID(model.Instructions_ID) ?? new PROJECT_INSTRUCTIONS();
                    if (modelView.Instructions_Path != model.Instructions_Path)
                    {
                        if (System.IO.File.Exists(Server.MapPath(modelView.Instructions_Path)))
                        {
                            System.IO.File.Delete(Server.MapPath(modelView.Instructions_Path));
                        }
                    }
                    if (_Instructions.EditInstructions(model) > 0)
                    {
                        statuCode = "200";
                        statuMessage = "配置信息更新成功";
                    }
                    else
                    {
                        statuCode = "300";
                        statuMessage = statuMessage == string.Empty ? "配置信息更新失败，请检查编号是否重复并重试！" : statuMessage;
                    }
                    exeType = "Edit";
                }
                else//实现添加
                {
                    int id = _Instructions.AddInstructions(model);
                    if (id > 0)
                    {
                        statuCode = "200";
                        statuMessage = "配置信息保存成功";
                    }
                    else
                    {
                        statuCode = "300";
                        statuMessage = statuMessage == string.Empty ? "保存失败，请检查编号是否重复！" : statuMessage; ;
                    }
                    exeType = "Create";
                }
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = exeType,
                data = model
            });
        }
    }
}
